#include "led_beep_module.h"

bool led1_flag = false;
bool led2_flag = false;
bool led3_flag = false; 
bool led4_flag = false;
bool beep_flag = false;

void led_on(int led)
{
    // 1.申请
    int fd = open("/sys/class/gpio/export", O_WRONLY);
    if(fd == -1) {
        perror("open");
        exit(-1);
    }
    write(fd, "120", 3);
    write(fd, "121", 3);
    write(fd, "123", 3);
    write(fd, "124", 3);
    fsync(fd);
    close(fd);

    switch(led) {
        case 1:
            // 设置方向
            fd = open("/sys/class/gpio/gpio120/direction", O_WRONLY);
            if(fd == -1) {
                perror("open");
                exit(-1);
            }

            write(fd, "out", 3);
            fsync(fd);
            close(fd);

            fd = open("/sys/class/gpio/gpio120/value", O_WRONLY);
            if(fd == -1) {
                perror("open");
                exit(-1);
            }
            write(fd, "1", 1);
            fsync(fd);
            close(fd);
            break;

        case 2:
            fd = open("/sys/class/gpio/gpio121/direction", O_WRONLY);
            if(fd == -1) {
                perror("open");
                exit(-1);
            }

            write(fd, "out", 3);    
            fsync(fd);
            close(fd);

            fd = open("/sys/class/gpio/gpio121/value", O_WRONLY);
            if(fd == -1) {
                perror("open");
                exit(-1);
            }
            write(fd, "1", 1);
            fsync(fd);
            close(fd);
            break;

        case 3:
            fd = open("/sys/class/gpio/gpio123/direction", O_WRONLY);
            if(fd == -1) {           
                perror("open");
                exit(-1);
            }
            write(fd, "out", 3);
            fsync(fd);
            close(fd);

            fd = open("/sys/class/gpio/gpio123/value", O_WRONLY);
            if(fd == -1) {
                perror("open");
                exit(-1);
            }
            write(fd, "1", 1);
            fsync(fd);
            close(fd);
            break;

        
        case 4:
            fd = open("/sys/class/gpio/gpio124/direction", O_WRONLY);
            if(fd == -1) {
                perror("open");
                exit(-1);
            }
            write(fd, "out", 3);
            fsync(fd);
            close(fd);

            fd = open("/sys/class/gpio/gpio124/value", O_WRONLY);
            if(fd == -1) {
                perror("open");
                exit(-1);
            }
            write(fd, "1", 1);
            fsync(fd);
            close(fd);
            break;

        default: break;
    }

    return ;
}

void led_off(int led)
{
    // 1.申请
    int fd = open("/sys/class/gpio/export", O_WRONLY);
    if(fd == -1) {
        perror("open");
        exit(-1);
    }
    write(fd, "120", 3);
    write(fd, "121", 3);
    write(fd, "123", 3);
    write(fd, "124", 3);
    fsync(fd);
    close(fd);

    switch(led) {
        case 1:
            // 设置方向
            fd = open("/sys/class/gpio/gpio120/direction", O_WRONLY);
            if(fd == -1) {
                perror("open");
                exit(-1);
            }

            write(fd, "out", 3);
            fsync(fd);
            close(fd);

            fd = open("/sys/class/gpio/gpio120/value", O_WRONLY);
            if(fd == -1) {
                perror("open");
                exit(-1);
            }
            write(fd, "0", 1);
            fsync(fd);
            close(fd);
            break;

        case 2:
            fd = open("/sys/class/gpio/gpio121/direction", O_WRONLY);
            if(fd == -1) {
                perror("open");
                exit(-1);
            }

            write(fd, "out", 3);    
            fsync(fd);
            close(fd);

            fd = open("/sys/class/gpio/gpio121/value", O_WRONLY);
            if(fd == -1) {
                perror("open");
                exit(-1);
            }
            write(fd, "0", 1);
            fsync(fd);
            close(fd);
            break;

        case 3:
            fd = open("/sys/class/gpio/gpio123/direction", O_WRONLY);
            if(fd == -1) {           
                perror("open");
                exit(-1);
            }
            write(fd, "out", 3);
            fsync(fd);
            close(fd);

            fd = open("/sys/class/gpio/gpio123/value", O_WRONLY);
            if(fd == -1) {
                perror("open");
                exit(-1);
            }
            write(fd, "0", 1);
            fsync(fd);
            close(fd);
            break;

        
        case 4:
            fd = open("/sys/class/gpio/gpio124/direction", O_WRONLY);
            if(fd == -1) {
                perror("open");
                exit(-1);
            }
            write(fd, "out", 3);
            fsync(fd);
            close(fd);

            fd = open("/sys/class/gpio/gpio124/value", O_WRONLY);
            if(fd == -1) {
                perror("open");
                exit(-1);
            }
            write(fd, "0", 1);
            fsync(fd);
            close(fd);
            break;

        default: break;
    }

    return ;
}

void beep_on()
{
    int fd = open("/sys/class/gpio/export",O_WRONLY);
    if(fd==-1){
        perror("open");
        exit(-1);
    }
    write(fd,"111",3);
    fsync(fd);
    close(fd);

    fd = open("/sys/class/gpio/gpio111/direction", O_WRONLY);
    if(fd == -1) {
        perror("open");
        exit(-1);
    }
    write(fd, "out", 3);
    fsync(fd);
    close(fd);

    fd = open("/sys/class/gpio/gpio111/value", O_WRONLY);
    if(fd == -1) {
        perror("open");
        exit(-1);
    }
    write(fd, "1", 1);
    fsync(fd);
    close(fd);
    return ;
}

void beep_off()
{
    int fd = open("/sys/class/gpio/export",O_WRONLY);
    if(fd==-1){
        perror("open");
        exit(-1);
    }
    write(fd,"111",3);
    fsync(fd);
    close(fd);

    fd = open("/sys/class/gpio/gpio111/direction", O_WRONLY);
    if(fd == -1) {
        perror("open");
        exit(-1);
    }
    write(fd, "out", 3);
    fsync(fd);
    close(fd);

    fd = open("/sys/class/gpio/gpio111/value", O_WRONLY);
    if(fd == -1) {
        perror("open");
        exit(-1);
    }
    write(fd, "0", 1);
    fsync(fd);
    close(fd);
    return ;
}